﻿using Sidvall.UI.Messaging;

namespace Sidvall.UI
{
    public class ApplicationManager : IApplicationManager
    {
        #region Public Members

        #region Status

        private ApplicationStatus _Status;
        public ApplicationStatus Status
        {
            get
            {
                return _Status;
            }
            set
            {
                if (_Status == value)
                    return;
                _Status = value;
                switch (_Status)
                {
                    case ApplicationStatus.NotInitialized:
                    case ApplicationStatus.Initializing:
                        break;
                    default:
                        OnApplicationEvent();
                        break;
                }
            }
        }

        #endregion
        #region SetupCompleted

        private bool _SetupCompleted;
        public bool SetupCompleted
        {
            get
            {
                return _SetupCompleted;
            }
            set
            {
                if (_SetupCompleted == value)
                    return;
                _SetupCompleted = value;
                OnApplicationEvent();
            }
        }

        #endregion
        #region Context

        private string _Context;
        public string Context
        {
            get
            {
                return _Context;
            }
            set
            {
                if (_Context == value)
                    return;
                _Context = value;
                OnApplicationEvent();
            }
        }

        #endregion

        #region OnApplicationEvent

        protected void OnApplicationEvent()
        {
            OnApplicationEvent(this.Status, this.Context, this.SetupCompleted);
        }
        protected void OnApplicationEvent(ApplicationStatus status, string context, bool setupCompleted)
        {
            var message = new ApplicationMessage(this)
            {
                Status = status,
                Context = context,
                SetupCompleted = setupCompleted,
            };
            OnApplicationEvent(message);
        }
        protected virtual void OnApplicationEvent(ApplicationMessage message)
        {
            EventManager.Publish(message);
        }

        #endregion

        #endregion
    }
}
